###爬取豆瓣Top250
#导入外部模块
import requests
import parsel
import time
import csv
#打开csv文件
f = open('./csv/豆瓣Top250.csv',mode = 'a',encoding = 'utf-8-sig',newline = '')
#写入表头
csv_writer = csv.DictWriter(f,fieldnames=[
    '电影排名','电影名称','电影评分','电影评价'
])
csv_writer.writeheader()
#当前页面数
num = 1
#循环遍历所有页面(每页25个电影信息，共10页)
for page in range(0,250,25):
    print(f"""
正在打印第{num}页的内容
""")
    #等待5秒再进行爬取
    time.sleep(5)
    #爬取的网页url
    url = f'https://movie.douban.com/top250?start={page}&filter='
    #指定请求头(伪装爬虫)
    headers = {
        'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36'
    }
    #request.get()发请求
    response = requests.get(url = url , headers = headers)
    #创建parsel选择器对象
    selector = parsel.Selector(response.text)
    #使用css选择器提取数据
    lis = selector.css('.grid_view li .item')
    for li in lis:
        rank = li.css('.pic em::text').get()
        title = li.css('.info .hd a span.title:nth-child(1)::text').get()
        rating_num = li.css('.info .bd .star span.rating_num::text').get()
        comment = li.css('.info .bd .quote span.inq::text').get()
        #写入表格行
        dic = {
            '电影排名' : rank,
            '电影名称': title,
            '电影评分': rating_num,
            '电影评价': comment
        }
        csv_writer.writerow(dic)
        #打印数据
        print(rank,title,rating_num,comment)
    #页数递增1
    num += 1
#关闭文件
f.close()